<template>
  <view class="my-page">
    <!-- 用户信息卡片 -->
    <view class="user-card">
      <view class="user-avatar" @tap="handleLoginClick">
        <image 
          :src="userInfo.avatar" 
          mode="aspectFill" 
          class="avatar-img"
        ></image>
      </view>
      
      <view class="user-info" @tap="handleLoginClick">
        <view class="user-name">{{ userInfo.name }}</view>
      </view>
      
      <view 
        v-if="isLogin" 
        class="edit-icon" 
        @tap="editUserInfo"
      >
        <uni-icons type="compose" size="24" color="#fff"></uni-icons>
      </view>

      <!-- 数字统计（仅登录后显示） -->
      <view v-if="isLogin" class="stats">
        <view class="stat-item" @tap="goToWishes">
          <text class="stat-number">{{ stats.orderCount }}</text>
          <text class="stat-label">愿望</text>
        </view>
        <view class="stat-item" @tap="goToMessages">
          <text class="stat-number">{{ stats.messageCount }}</text>
          <text class="stat-label">消息</text>
        </view>
        <view class="stat-item" @tap="goToCoupons">
          <text class="stat-number">{{ stats.couponCount }}</text>
          <text class="stat-label">优惠券</text>
        </view>
      </view>
    </view>

    <!-- 快捷功能区域 -->
    <view class="quick-actions">
      <view 
        v-for="action in quickActions" 
        :key="action.id"
        class="action-item"
        @tap="navigateTo(action.path, action.requireLogin)"
      >
        <uni-icons :type="action.icon" size="36" color="#00C853"></uni-icons>
        <text class="action-text">{{ action.label }}</text>
      </view>
    </view>

    <!-- 功能菜单区域 -->
    <view class="menu-section">
      <view 
        v-for="item in menuItems" 
        :key="item.id"
        class="menu-item"
        @tap="navigateTo(item.path, item.requireLogin)"
      >
        <view class="menu-icon">
          <uni-icons :type="item.icon" size="24" :color="item.iconColor"></uni-icons>
        </view>
        <view class="menu-text">{{ item.label }}</view>
        <view class="menu-arrow">
          <uni-icons type="arrowright" size="16" color="#ccc"></uni-icons>
        </view>
      </view>
    </view>

    <!-- 退出登录按钮 -->
    <view v-if="isLogin" class="logout-container">
      <button class="logout-btn" @tap="handleLogout">
        <uni-icons type="person" size="20" color="#666"></uni-icons>
        <text class="logout-text">退出登录</text>
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { onShow } from '@dcloudio/uni-app';
import { useUserStore } from '@/store/modules/user';
import { useUserProfile } from '@/composables/useUserProfile';
import { logout as apiLogout } from '@/api/auth';

const userStore = useUserStore();
const {
  isLogin,
  userInfo,
  stats,
  quickActions,
  menuItems,
  loadUserStats,
  navigateTo,
  handleLoginClick,
  editUserInfo
} = useUserProfile();

// 我的愿望
const goToWishes = () => {
  if (!isLogin.value) {
    uni.showToast({
      title: '请先登录',
      icon: 'none'
    });
    return;
  }
  uni.navigateTo({
    url: '/pages/my/wishList/wishList'
  });
};

// 消息中心
const goToMessages = () => {
  if (!isLogin.value) {
    uni.showToast({
      title: '请先登录',
      icon: 'none'
    });
    return;
  }
  uni.navigateTo({
    url: '/pages/my/messages/messages'
  });
};

// 我的优惠券
const goToCoupons = () => {
  if (!isLogin.value) {
    uni.showToast({
      title: '请先登录',
      icon: 'none'
    });
    return;
  }
  uni.navigateTo({
    url: '/pages/my/coupon/coupon'
  });
};

// 退出登录
const handleLogout = () => {
  uni.showModal({
    title: '提示',
    content: '确定要退出登录吗?',
    success: async (res) => {
      if (res.confirm) {
        try {
          uni.showLoading({ title: '退出中...' });

          // 调用后端退出接口
          await apiLogout();

          // 清除本地数据
          userStore.logout();

          uni.hideLoading();
          uni.showToast({
            title: '已退出登录',
            icon: 'success'
          });

          // 跳转到首页
          setTimeout(() => {
            uni.switchTab({
              url: '/pages/index/index'
            });
          }, 1000);

        } catch (error) {
          uni.hideLoading();
          console.error('退出登录失败:', error);

          // 即使接口失败也要清除本地数据
          userStore.logout();

          uni.showToast({
            title: '已退出登录',
            icon: 'success'
          });

          setTimeout(() => {
            uni.switchTab({
              url: '/pages/index/index'
            });
          }, 1000);
        }
      }
    }
  });
};

// 页面显示时刷新数据
onShow(() => {
  if (isLogin.value && userStore.userInfo?.customerId) {
    userStore.fetchUserInfo();
    loadUserStats();
  }
});
</script>

<style scoped>
.my-page {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding: 20rpx;
}

/* 用户卡片 */
.user-card {
  background: linear-gradient(135deg, #00C853, #009688);
  border-radius: 20rpx;
  padding: 40rpx 30rpx;
  display: flex;
  flex-direction: column;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 200, 83, 0.3);
  position: relative;
}

.user-avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 20rpx;
  border: 4rpx solid rgba(255, 255, 255, 0.3);
  background-color: #f0f0f0;
}

.avatar-img {
  width: 100%;
  height: 100%;
}

.user-info {
  margin-bottom: 20rpx;
}

.user-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
}

.edit-icon {
  position: absolute;
  top: 30rpx;
  right: 30rpx;
  width: 60rpx;
  height: 60rpx;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stats {
  display: flex;
  justify-content: space-around;
  padding-top: 20rpx;
  border-top: 1rpx solid rgba(255, 255, 255, 0.3);
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8rpx;
  cursor: pointer;
  transition: transform 0.2s;

}

.stat-item:active {
  transform: scale(0.95);
}

.stat-number {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
}

.stat-label {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.9);
}

/* 快捷功能区 */
.quick-actions {
  display: flex;
  background-color: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.action-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10rpx;
}

.action-text {
  font-size: 24rpx;
  color: #333;
}

/* 功能菜单区 */
.menu-section {
  background-color: #fff;
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-icon {
  width: 50rpx;
  height: 50rpx;
  background-color: #f0f9f0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20rpx;
}

.menu-text {
  flex: 1;
  font-size: 28rpx;
  color: #333;
}

.menu-arrow {
  margin-left: 20rpx;
}

/* 退出登录按钮 */
.logout-container {
  padding: 20rpx 0;
}

.logout-btn {
  width: 100%;
  height: 88rpx;
  background-color: #fff;
  border-radius: 44rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15rpx;
  border: none;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.logout-text {
  font-size: 28rpx;
  color: #666;
}
</style>